GtkFontChooserDialog: Define children with a GtkBuilder template
authorTristan Van Berkom <tristanvb@openismus.com>
Tue, 26 Mar 2013 10:13:12 +0000 (19:13 +0900)
committerTristan Van Berkom <tristanvb@openismus.com>
Mon, 8 Apr 2013 12:19:29 +0000 (21:19 +0900)
gtk/Makefile.am
gtk/gtk.gresource.xml
gtk/gtkfontchooserdialog.c
gtk/gtkfontchooserdialog.ui [new file with mode: 0644]
gtk/tests/templates.c

index 7e5b0d2264186b68a1fe1752d3f98fb56860bf11..426c4d78afde1f4434baf238eaed1a435ae018fe 100644 (file)
@@ -1104,6 +1104,7 @@ COMPOSITE_TEMPLATES =                     \
        gtkdialog.ui                    \
        gtkfilechooserdefault.ui        \
        gtkfilechooserdialog.ui         \
+       gtkfontchooserdialog.ui         \
        gtkfontchooserwidget.ui         \
        gtkinfobar.ui                   \
        gtklockbutton.ui                \
index 9ca8d7a819c5afe5b3e09f5942e2fd7b876817e0..43f0af03be1c858f1f0545fd72b934c93ac67f8e 100644 (file)
@@ -19,6 +19,7 @@
     <file compressed="true">gtkdialog.ui</file>
     <file compressed="true">gtkfilechooserdefault.ui</file>
     <file compressed="true">gtkfilechooserdialog.ui</file>
+    <file compressed="true">gtkfontchooserdialog.ui</file>
     <file compressed="true">gtkfontchooserwidget.ui</file>
     <file compressed="true">gtkinfobar.ui</file>
     <file compressed="true">gtklockbutton.ui</file>
index 67244407c4ed6149c7ecda65bf1c4ca617ab4755..d1209ded9f96e4edff4210dbec2c80e63ab5299a 100644 (file)
@@ -109,81 +109,57 @@ gtk_font_chooser_dialog_get_property (GObject      *object,
     }
 }
 
+static void
+font_activated_cb (GtkFontChooser *fontchooser,
+                   const gchar    *fontname,
+                   gpointer        user_data)
+{
+  GtkDialog *dialog = user_data;
+
+  gtk_dialog_response (dialog, GTK_RESPONSE_OK);
+}
+
 static void
 gtk_font_chooser_dialog_class_init (GtkFontChooserDialogClass *klass)
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
   gobject_class->get_property = gtk_font_chooser_dialog_get_property;
   gobject_class->set_property = gtk_font_chooser_dialog_set_property;
 
   _gtk_font_chooser_install_properties (gobject_class);
 
-  g_type_class_add_private (klass, sizeof (GtkFontChooserDialogPrivate));
-}
+  /* Bind class to template
+   */
+  gtk_widget_class_set_template_from_resource (widget_class,
+                                              "/org/gtk/libgtk/gtkfontchooserdialog.ui");
 
-static void
-font_activated_cb (GtkFontChooser *fontchooser,
-                   const gchar    *fontname,
-                   gpointer        user_data)
-{
-  GtkDialog *dialog = user_data;
+  gtk_widget_class_bind_child (widget_class, GtkFontChooserDialogPrivate, fontchooser);
+  gtk_widget_class_bind_child (widget_class, GtkFontChooserDialogPrivate, select_button);
+  gtk_widget_class_bind_child (widget_class, GtkFontChooserDialogPrivate, cancel_button);
+  gtk_widget_class_bind_callback (widget_class, font_activated_cb);
 
-  gtk_dialog_response (dialog, GTK_RESPONSE_OK);
+  g_type_class_add_private (klass, sizeof (GtkFontChooserDialogPrivate));
 }
 
 static void
 gtk_font_chooser_dialog_init (GtkFontChooserDialog *fontchooserdiag)
 {
   GtkFontChooserDialogPrivate *priv;
-  GtkDialog *dialog = GTK_DIALOG (fontchooserdiag);
-  GtkWidget *action_area, *content_area;
 
   fontchooserdiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooserdiag,
                                                        GTK_TYPE_FONT_CHOOSER_DIALOG,
                                                        GtkFontChooserDialogPrivate);
   priv = fontchooserdiag->priv;
 
-  content_area = gtk_dialog_get_content_area (dialog);
-  action_area = gtk_dialog_get_action_area (dialog);
-
-  gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
-  gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
-  gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
-  gtk_box_set_spacing (GTK_BOX (action_area), 6);
-
-  gtk_widget_push_composite_child ();
-
-  gtk_window_set_resizable (GTK_WINDOW (fontchooserdiag), TRUE);
-
-  /* Create the content area */
-  priv->fontchooser = gtk_font_chooser_widget_new ();
-  gtk_container_set_border_width (GTK_CONTAINER (priv->fontchooser), 5);
-  gtk_widget_show (priv->fontchooser);
-  gtk_box_pack_start (GTK_BOX (content_area),
-                      priv->fontchooser, TRUE, TRUE, 0);
-
-  g_signal_connect (priv->fontchooser, "font-activated",
-                    G_CALLBACK (font_activated_cb), dialog);
-
-  /* Create the action area */
-  priv->cancel_button = gtk_dialog_add_button (dialog,
-                                               GTK_STOCK_CANCEL,
-                                               GTK_RESPONSE_CANCEL);
-  priv->select_button = gtk_dialog_add_button (dialog,
-                                               _("_Select"),
-                                               GTK_RESPONSE_OK);
-  gtk_widget_grab_default (priv->select_button);
+  gtk_widget_init_template (GTK_WIDGET (fontchooserdiag));
 
   gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontchooserdiag),
                                            GTK_RESPONSE_OK,
                                            GTK_RESPONSE_CANCEL,
                                            -1);
 
-  gtk_window_set_title (GTK_WINDOW (fontchooserdiag), _("Font Selection"));
-
-  gtk_widget_pop_composite_child ();
-
   _gtk_font_chooser_set_delegate (GTK_FONT_CHOOSER (fontchooserdiag),
                                   GTK_FONT_CHOOSER (priv->fontchooser));
 }
diff --git a/gtk/gtkfontchooserdialog.ui b/gtk/gtkfontchooserdialog.ui
new file mode 100644 (file)
index 0000000..8878052
--- /dev/null
@@ -0,0 +1,78 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface domain="gtk30">
+  <!-- interface-requires gtk+ 3.10 -->
+  <template class="GtkFontChooserDialog" parent="GtkDialog">
+    <property name="can_focus">False</property>
+    <property name="border_width">5</property>
+    <property name="title" translatable="yes">Font Selection</property>
+    <property name="type_hint">dialog</property>
+    <child internal-child="vbox">
+      <object class="GtkBox" id="dialog-vbox1">
+        <property name="can_focus">False</property>
+        <property name="orientation">vertical</property>
+        <property name="spacing">2</property>
+        <child internal-child="action_area">
+          <object class="GtkButtonBox" id="dialog-action_area1">
+            <property name="can_focus">False</property>
+            <property name="layout_style">end</property>
+            <child>
+              <object class="GtkButton" id="cancel_button">
+                <property name="label">gtk-cancel</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <property name="use_stock">True</property>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">True</property>
+                <property name="position">0</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkButton" id="select_button">
+                <property name="label" translatable="yes">_Select</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="can_default">True</property>
+                <property name="has_default">True</property>
+                <property name="receives_default">True</property>
+                <property name="use_underline">True</property>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">True</property>
+                <property name="position">1</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="pack_type">end</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkFontChooserWidget" id="fontchooser">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="border_width">5</property>
+            <property name="orientation">vertical</property>
+            <property name="spacing">6</property>
+            <signal name="font-activated" handler="font_activated_cb" swapped="no"/>
+          </object>
+          <packing>
+            <property name="expand">True</property>
+            <property name="fill">True</property>
+            <property name="position">1</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+    <action-widgets>
+      <action-widget response="-6">cancel_button</action-widget>
+      <action-widget response="-5">select_button</action-widget>
+    </action-widgets>
+  </template>
+</interface>
index 6c61bdacc28ab0cdb00a0eb282be436759be2f9a..cdbaa2ec4494eeef1f759a1c0d15b6f81e57cec4 100644 (file)
@@ -234,6 +234,16 @@ test_font_chooser_widget_basic (void)
   gtk_widget_destroy (widget);
 }
 
+static void
+test_font_chooser_dialog_basic (void)
+{
+  GtkWidget *widget;
+
+  widget = gtk_font_chooser_dialog_new ("Choose a font !", NULL);
+  g_assert (GTK_IS_FONT_CHOOSER_DIALOG (widget));
+  gtk_widget_destroy (widget);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -261,6 +271,7 @@ main (int argc, char **argv)
   g_test_add_func ("/Template/GtkFileChooserWidget/Basic", test_file_chooser_widget_basic);
   g_test_add_func ("/Template/GtkFileChooserDialog/Basic", test_file_chooser_dialog_basic);
   g_test_add_func ("/Template/GtkFontChooserWidget/Basic", test_font_chooser_widget_basic);
+  g_test_add_func ("/Template/GtkFontChooserDialog/Basic", test_font_chooser_dialog_basic);
 
   return g_test_run();
 }